//存放主要交互逻辑代码
//javascript模块化
var seckill = {
    //封装秒杀相关的ajax的地址
    URL: {
        now : function () {
            return '/seckill/time/now';
        },
        exposer : function (seckillId) {
            return '/seckill/' + seckillId + '/exposer';
        },
        exection : function (seckillId, md5) {
            return '/seckill/' + seckillId + '/' + md5 + '/exection';
        }
    },
    //处理秒杀逻辑
    handlerSeckill : function (seckillId, node) {
        //隐藏节点
        node.hide().html('<button class="btn btn-primary btn.lg" id="killBtn">开始秒杀</button>');
        $.post(seckill.URL.exposer(seckillId), {}, function (result) {
            //在回调函数中，执行交互流程
            if (result && result['success']) {
                var exposer = result['data'];
                if (exposer['exposed']) {
                    //开启秒杀
                    //获取秒杀地址
                    var md5 = exposer['md5'];
                    var killUrl = seckill.URL.exection(seckillId, md5);
                    console.log("killUrl:" + killUrl);
                    //绑定一次点击事件
                    $('#killBtn'). one('click', function () {
                        //绑定执行秒杀请求操作
                        //1.禁用按钮
                        $(this).addClass('disabled');
                        //2.发送秒杀的请求
                        $.post(killUrl, {}, function (result) {
                            if (result && result['success']) {
                                var killResult = result['data'];
                                var status = killResult['status'];
                                var statusInfo = killResult['statusInfo'];
                                //3.显示秒杀结果
                                node.html('<span class="label label-success">' + statusInfo + '</span>');
                            }
                        });
                    });
                    node.show();
                } else {
                    //未开启秒杀
                    var nowTime = exposer['nowTime'];
                    var startTime = exposer['startTime'];
                    var endTime = exposer['endTime'];
                    //重新计算计时逻辑
                    seckill.countdown(seckillId, nowTime, startTime, endTime);
                }
            } else {
                console.log('result' + result);
            }
        })
    },
    //验证手机号
    validatePhone : function(phone) {
        if (phone && phone.length==11 && !isNaN(phone)) {
            return true;
        } else {
            return false;
        }
    },
    countdown : function (seckillId, nowTime, startTime, endTime) {
        var seckillBox = $('#seckill-box');
        //时间判断
        if (nowTime > endTime) {
            //秒杀结束
            seckillBox.html('秒杀结束！');
        } else if (nowTime < startTime) {
            //秒杀未开始,计时
            var killTime = new Date(startTime + 1000);
            seckillBox.countdown(killTime, function (envent) {
                //时间格式
                var format = event.strftime('秒杀倒计时：%D天 %H时 %M分 %S秒');
                seckillBox.html(format);
                //时间完成后回调事件
            }).on('finish.countdown', function(){
                //获取秒杀地址，控制实现逻辑，执行秒杀
                seckill.handlerSeckill(seckillId, seckillBox);
            });
        } else {
            //秒杀开始
            seckill.handlerSeckill(seckillId, seckillBox);
        }
    },
    //详情页秒杀逻辑
    detail: {
        //详情页初始化
        init: function(params) {
            //手机验证和登录，计时交互
            //规划我们的交互流程
            //在cookie中查找手机号
            var user_phone = $.cookie('user_phone');
            //验证手机号
            if (!seckill.validatePhone(user_phone)) {
                //绑定手机号
                //控制输出
                var userPhoneModal = $('#userPhoneModal');
                userPhoneModal.modal({
                    //显示弹出层
                    show: true,
                    //禁止位置关闭
                    backdrop: 'static',
                    //关闭键盘事件
                    keyboard: false
                });
                $('#userPhoneBtn').click(function () {
                    var inputPhone = $('#userPhoneKey').val();
                    console.log('inputPhone=' + imputPhone);    //TODO
                    if (seckil.validatePhone(inputPhone)) {
                        //将电话写入cookie
                        $.cookie('userPhone', inputPhone, {expires:7, path:'seckill'});
                        //刷新页面
                        window.location.reload();
                    } else {
                        $('userPhoneMessage').hide().html('<label class="label label-danger">手机号错误！</label>>'.show(300));
                    }
                });
            }
            //已经登录
            //计时交互
            var seckill_start_time = params['seckill_start_time'];
            var seckill_end_time = params['seckill_end_time'];
            var seckill_id = params['seckill_id'];
            $.get(seckill.URL.now(), {}, function (result) {
                if(result && result['success']) {
                    var nowTime = result['data'];
                    //时间判断
                    countdown(seckill_id, nowTime, seckill_start_time, seckill_end_time);
                } else {
                    console.log('result:' + result);
                }
            });
        }
    }
}